<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="产品名称" prop="productName">
        <el-input v-model="queryParams.productName" placeholder="请输入产品名称" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="供应商" prop="provide">
        <el-input v-model="queryParams.provide" placeholder="请输入供应商" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="所属仓库" prop="warehouse">
        <el-input v-model="queryParams.warehouse" placeholder="请输入所属仓库" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>

      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd" v-hasPermi="['system:audit:add']"
         >新增</el-button>
      </el-col>
      <!-- <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['system:audit:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['system:audit:remove']"
        >删除</el-button>
      </el-col> -->
      <el-col :span="1.5">
        <el-button type="warning" plain icon="el-icon-download" size="mini" @click="handleExport"
        v-hasPermi="['system:audit:Export']"
         >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="auditList">
      <!-- <el-table-column type="selection" width="55" align="center" /> -->
      <el-table-column label="序号" align="center" prop="index" />
      <el-table-column label="产品编号" align="center" prop="productCode" width="180" />
      <el-table-column label="产品名称" align="center" prop="productName" />
      <el-table-column label="价格" align="center" prop="money" />
      <el-table-column label="规格" align="center" prop="model" width="150" />
      <el-table-column label="型号" align="center" prop="specs" />
      <el-table-column label="质保期" align="center" prop="warranty" />
      <el-table-column label="单位" align="center" prop="unit" />
      <el-table-column label="品牌" align="center" prop="brand" width="150" />
      <el-table-column label="生产厂商" align="center" prop="provideCompany" />
      <el-table-column label="产地" align="center" prop="origin" />
      <el-table-column label="供应商" align="center" prop="provide" />
      <el-table-column label="结算模式" align="center" prop="settleMent" />
      <el-table-column label="是否正式产品" align="center" prop="isFormal" />
      <el-table-column label="是否采购" align="center" prop="isProcure" />
      <el-table-column label="所属仓库" align="center" prop="warehouse" />
      <el-table-column label="进价" align="center" prop="planPrice" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width" width="150">
        <template slot-scope="scope">
          <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)"
          v-hasPermi="['system:audit:edit']"
            >修改</el-button>
          <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)"
          v-hasPermi="['system:audit:remove']"
            >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
      @pagination="getList" />

    <!-- 添加或修改审计基础对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="900px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="120px">
        <el-row>
          <el-col :span="12">
            <el-form-item label="产品编号" prop="productCode">
              <el-input v-model="form.productCode" placeholder="请输入产品编号" />
            </el-form-item>
            <el-form-item label="产品名称" prop="productName">
              <el-input v-model="form.productName" placeholder="请输入产品名称" />
            </el-form-item>
            <el-form-item label="价格" prop="money">
              <el-input v-model="form.money" placeholder="请输入价格" />
            </el-form-item>
            <el-form-item label="规格" prop="model">
              <el-input v-model="form.model" placeholder="请输入规格" />
            </el-form-item>
            <el-form-item label="型号" prop="specs">
              <el-input v-model="form.specs" placeholder="请输入型号" />
            </el-form-item>
            <el-form-item label="质保期" prop="warranty">
              <el-input v-model="form.warranty" placeholder="请输入质保期" />
            </el-form-item>
            <el-form-item label="单位" prop="unit">
              <el-input v-model="form.unit" placeholder="请输入单位" />
            </el-form-item>
            <el-form-item label="品牌" prop="brand">
              <el-input v-model="form.brand" placeholder="请输入品牌" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="生产厂商" prop="provideCompany">
              <el-input v-model="form.provideCompany" placeholder="请输入生产厂商" />
            </el-form-item>
            <el-form-item label="产地" prop="origin">
              <el-input v-model="form.origin" placeholder="请输入产地" />
            </el-form-item>
            <el-form-item label="供应商" prop="provide">
              <el-input v-model="form.provide" placeholder="请输入供应商" />
            </el-form-item>
            <el-form-item label="结算模式" prop="settleMent">
              <el-input v-model="form.settleMent" placeholder="请输入结算模式" />
            </el-form-item>
            <el-form-item label="是否正式产品" prop="isFormal">
              <el-input v-model="form.isFormal" placeholder="请输入是否正式产品" />
            </el-form-item>
            <el-form-item label="是否采购" prop="isProcure">
              <el-input v-model="form.isProcure" placeholder="请输入是否采购" />
            </el-form-item>
            <el-form-item label="所属仓库" prop="warehouse">
              <el-input v-model="form.warehouse" placeholder="请输入所属仓库" />
            </el-form-item>
            <el-form-item label="进价" prop="planPrice">
              <el-input v-model="form.planPrice" placeholder="请输入进价" />
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer" style="text-align: center;">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listAudit, getAudit, delAudit, addAudit, updateAudit } from "@/api/system/audit";

export default {
  name: "Audit",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 审计基础表格数据
      auditList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        productName: null,
        provide: null,
        warehouse: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        productCode: [
          { required: true, message: '请输入产品编号', trigger: 'blur' }
        ],
        productName: [
          { required: true, message: '请输入产品名称', trigger: 'blur' }
        ],
        money: [
          { required: true, message: '请输入价格', trigger: 'blur' }
        ],
        model: [
          { required: true, message: '请输入规格', trigger: 'blur' }
        ],
        specs: [
          { required: true, message: '请输入型号', trigger: 'blur' }
        ],
        warranty: [
          { required: true, message: '请输入质保期', trigger: 'blur' }
        ],
        unit: [
          { required: true, message: '请输入单位', trigger: 'blur' }
        ],
        brand: [
          { required: true, message: '请输入品牌', trigger: 'blur' }
        ],
        provideCompany: [
          { required: true, message: '请输入生产厂商', trigger: 'blur' }
        ],
        origin: [
          { required: true, message: '请输入产地', trigger: 'blur' }
        ],
        provide: [
          { required: true, message: '请输入供应商', trigger: 'blur' }
        ],
        settleMent: [
          { required: true, message: '请输入结算模式', trigger: 'blur' }
        ],
        isFormal: [
          { required: true, message: '请输入是否正式产品', trigger: 'blur' }
        ],
        isProcure: [
          { required: true, message: '请输入是否采购', trigger: 'blur' }
        ],
        warehouse: [
          { required: true, message: '请输入所属仓库', trigger: 'blur' }
        ],
        planPrice: [
          { required: true, message: '请输入进价', trigger: 'blur' }
        ]
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询审计基础列表 */
    getList() {
      this.loading = true;
      listAudit(this.queryParams).then(response => {
        this.auditList = response.rows;
        this.auditList.forEach((item, index) => {
          item.index = (this.queryParams.pageNum - 1) * this.queryParams.pageSize + index + 1;
        })
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        productCode: null,
        productName: null,
        money: null,
        model: null,
        specs: null,
        warranty: null,
        unit: null,
        brand: null,
        provideCompany: null,
        origin: null,
        provide: null,
        settleMent: null,
        isFormal: null,
        isProcure: null,
        warehouse: null,
        planPrice: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },

    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加审计基础";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getAudit(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改审计基础";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateAudit(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addAudit(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除审计基础编号为"' + ids + '"的数据项？').then(function () {
        return delAudit(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => { });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('sys/audit/export', {
        ...this.queryParams
      }, `审计配置表${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
<style lang="scss" scoped></style>
